﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThanhTu.GNCMedia.KQXS.DataAccess.DAL;
using System.Data;
using ThanhTu.GNCMedia.KQXS.Common;
using ThanhTu.GNCMedia.KQXS.DataAccess.DO;

namespace WebUI.Admin.UserControls
{
    public partial class ResultLotteryList : System.Web.UI.UserControl
    {
        private int pageSize = 5;
        private int pageIndex = 1;
        private int rowCount = 0;
        protected void Page_Init(object sender, EventArgs e)
        {
            
        }

        private void LoadAll()
        {
            ResultLotteryDAL resultLottery = new ResultLotteryDAL();
            int rowCount = 0;
            DataTable dt = resultLottery.SelectAll_KQ_User(1, 10, ref rowCount);
            RepeaterResultLotteryList.DataSource = dt;
            RepeaterResultLotteryList.DataBind();
            for (int i = 1; i < rowCount/pageSize; i++)
            {
                ddPage.Items.Add(i.ToString());
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadAll();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("AddNewData.aspx");
        }

        protected void RepeaterResultLotteryList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ResultLotteryDAL resultLotteryDAL = new ResultLotteryDAL();
              int checkDEL=  resultLotteryDAL.delete(e.CommandArgument.ToString(), Convert.ToDateTime(e.CommandName.ToString()));
              if (checkDEL > 0)
              {
                  Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "javascript:alert('XÓA bản ghi  Thành công);", true);
              }
              else
              {
                  Page.ClientScript.RegisterStartupScript(this.GetType(), "ShowStatus", "javascript:alert('LỖI XÓA ,XIN VUI LÒNG THỬ LẠI);", true);
              }
            }
        }

        protected void lbtn_first_resultlottery_Click(object sender, EventArgs e)
        {
            LoadAll();
        }

        protected void lbtn_pre_resultlottery_Click(object sender, EventArgs e)
        {
            ResultLotteryDAL messengerSyntaxDAL = new ResultLotteryDAL();
            DataTable dt = messengerSyntaxDAL.SelectAll_KQ_User(pageIndex, pageSize, ref rowCount);
            int totalrow = rowCount;
            pageIndex = totalrow / pageSize;

            if (pageIndex > 1)
            {
                pageIndex--;
                DataTable dt1 = messengerSyntaxDAL.SelectAll_KQ_User(pageIndex, pageSize, ref rowCount);
                RepeaterResultLotteryList.DataSource = dt;
                RepeaterResultLotteryList.DataBind();
            }
        }

        protected void lbtntnext_resultlottery_Click(object sender, EventArgs e)
        {
            pageIndex += 1;
            pageSize += 5;
            ResultLotteryDAL messengerSyntaxDAL = new ResultLotteryDAL();
            DataTable dt = messengerSyntaxDAL.SelectAll_KQ_User(pageIndex, pageSize, ref rowCount);
            RepeaterResultLotteryList.DataSource = dt;
            RepeaterResultLotteryList.DataBind();
            pageIndex += 1;
        }

        protected void lbtlast_resultlottery_Click(object sender, EventArgs e)
        {
            ResultLotteryDAL messengerSyntaxDAL = new ResultLotteryDAL();
            DataTable dt = messengerSyntaxDAL.SelectAll_KQ_User(pageIndex, pageSize, ref rowCount);
            RepeaterResultLotteryList.DataSource = dt;
            RepeaterResultLotteryList.DataBind();
            int lastpage = rowCount;
            DataTable dt1 = messengerSyntaxDAL.SelectAll_KQ_User(pageIndex, pageSize, ref rowCount);
            RepeaterResultLotteryList.DataSource = dt1;
            RepeaterResultLotteryList.DataBind();
        }

        protected void ddPage_SelectedIndexChanged(object sender, EventArgs e)
        {

            int runToPage = Convert.ToInt32(ddPage.SelectedItem.Text);
            MessengerSyntaxDAL messengerSyntaxDAL = new MessengerSyntaxDAL();
            DataTable dt = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, pageSize, ref rowCount);
            RepeaterResultLotteryList.DataSource = dt;
            RepeaterResultLotteryList.DataBind();
            if (runToPage == rowCount / runToPage)
            {
                DataTable dt2 = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, rowCount, ref rowCount);
                RepeaterResultLotteryList.DataSource = dt2;
                RepeaterResultLotteryList.DataBind();
            }
            else
            {
                if (runToPage == 1)
                {
                    DataTable dt3 = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, pageSize, ref rowCount);
                    RepeaterResultLotteryList.DataSource = dt3;
                    RepeaterResultLotteryList.DataBind();
                }
                else
                {
                    int dump = rowCount / runToPage;
                    DataTable dt1 = messengerSyntaxDAL.SelectAll_Messenger(pageIndex, dump, ref rowCount);
                    RepeaterResultLotteryList.DataSource = dt1;
                    RepeaterResultLotteryList.DataBind();
                }
            }
        }
    }
}